package lar.game.physics;

public class Position
{
	private final long x;
	private final long y;
	
	private static long Round(double d)
	{
		return Math.round(d * 1000.0);
	}
	
	private static double RetVal(long i)
	{
		return i / 1000.0;
	}
	
	public Position(double x, double y)
	{
		this.x = Round(x);
		this.y = Round(y);
	}
	
	public Position(int x, int y)
	{
		this.x = Round(ToWorld(x));
		this.y = Round(ToWorld(y));
	}
	
	public Position (String init)
	{
		String[] strArr = init.split("_");
		x = Long.parseLong(strArr[0]);
		y = Long.parseLong(strArr[1]);
	}
	
	public String Serialize()
	{
		return "" + x + "_" + y;
	}
	
	public double PosX()
	{
		return RetVal(x);
	}
	
	public double PosY()
	{
		return RetVal(y);
	}
	
	public int TerrainX()
	{
		return ToTerrain(RetVal(x));
	}
	
	public int TerrainY()
	{
		return ToTerrain(RetVal(y));
	}
	
	public Position Add(Vector speed)
	{
		return new Position(RetVal(x) + speed.GetX(), RetVal(y) + speed.GetY());
	}
	
	public static int ToTerrain(double element)
	{
		return (int) Math.floor(element);
	}
	
	public static double ToWorld(int element)
	{
		return element + 0.5;
	}
	
	public Position MirrorX(double axis)
	{
		return new Position(axis*2 - RetVal(x), RetVal(y));
	}
	
	public Position MirrorY(double axis)
	{
		return new Position(RetVal(x), axis*2 - RetVal(y));
	}
	
	public Vector VectorTo(Position other)
	{
		return new Vector(RetVal(other.x - x), RetVal(other.y - y));
	}
	
	@Override
	public boolean equals(Object other)
	{
		if(other instanceof Position)
		{
			if( ((Position) other).PosX() == this.PosX()  
					&& ((Position) other).PosY() == this.PosY())
				return true;
		}
		return false;		
	}
	
	@Override
	public String toString()
	{
		return "(" + PosX() + "," + PosY() + ")";
	}
}
